﻿using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YmSoft.WPF.Modbus
{
    public class MVWriteMultipleCoils : MVMessageBase
    {
        public MVWriteMultipleCoils(MVModbus mvModbus) : base(mvModbus) { }


        ushort startAddress = 1;
        public ushort StartAddress
        {
            get
            {
                return this.startAddress;
            }

            set
            {
                if (value != this.startAddress)
                {
                    this.startAddress = value;
                    NotifyPropertyChanged("StartAddress");
                }
            }
        }

        string datasString = null;
        public string DatasString
        {
            get
            {
                return this.datasString;
            }

            set
            {
                if (value != this.datasString)
                {
                    this.datasString = value;
                    NotifyPropertyChanged("DatasString");
                    if (string.IsNullOrEmpty(datasString))
                    {
                        Datas = null;
                        return;
                    }
                    Datas = datasString.Replace(" ", "").Select(p => p == '1').ToArray();
                }
            }
        }

        bool[] Datas { get; set; }
        protected override void SendMessage(IModbusSerialMaster master)
        {
            master.WriteMultipleCoils(SlaveAddress, StartAddress, Datas);
        }
    }
}
